The Storage Kit Table of Contents | The Storage Kit Index |
Derived from: BNode, BEntryList
Declared in: be/storage/Directory.h
Library: libbe.so
A BDirectory object gives you access to the contents of a directory. A BDirectory's primary features are:
Unlike the other BNode classes, a BDirectory knows its own entry (GetEntry()), and can be initialized with a node_ref structure.
The BDirectory functions that let you iterate over a directory's entries are inherited from BEntryList:
|
For the basic story on these functions, see the BEntryList class and the function descriptions below. In addition to the info you'll find there, you should be aware of the following:
To create a new directory, you can use BDirectory's CreateDirectory() function. The function creates a single new directory as identified by its argument. The new directory will be a subdirectory of the invoked-upon BDirectory's directory.
You can also create an entire path full of new directories through the global create_directory() function. This convenient function attempts to create all "missing" directories along the path that you pass in.
The find_directory() function gives you the pathnames for pre-defined directories. These directories, such as those that store Be-supplied applications and user-defined preferences settings, are represented by directory_which constants. These constants are not strings; you can't use them directly. You have to pass them through find_directory().
Note that the BDirectory class itself doesn't let you find directories on the basis of the directory_which constants—you have to use the find_directory() function (which is documented at the end of this class description).
|
You can monitor changes to the contents of a directory by passing a BDirectory's node_ref and the B_WATCH_DIRECTORY flag to the Node Monitor's watch_node() function. As with all invocations of watch_node(), you also have to pass a BMessenger (the "target") that will receive the Node Monitor notifications; here, we use be_app_messenger:
BDirectory dir("/boot/home"); node_ref nref; status_t err; if (dir.InitCheck() == B_OK) { dir.GetNodeRef(&nref); err = watch_node(&nref, B_WATCH_DIRECTORY, be_app_messenger); if (err != B_OK) /* handle the error */ }
The following changes to the monitored directory cause BMessages to be sent to the target. The what field for all Node Monitor messages is B_NODE_MONITOR; the "opcode" field (an integer code) describes the activity:
The B_WATCH_DIRECTORY flag (by itself) doesn't monitor changes to the directory's own entry. For example, if you change the name of the directory that you're monitoring, the target isn't sent a message. If you want a BDirectory to watch changes to itself, you have to throw in one of the other Node Monitor flags (B_WATCH_NAME, B_WATCH_STAT, or B_WATCH_ATTR).
The other fields in the Node Monitor message describe the entry that changed. The set of fields depends on the opcode (the following is a summary of the list given in "Notification Messages" in the Node Monitor documentation):
Field | Type | Description |
---|---|---|
"device" | B_INT32_TYPE | dev_t of the directory's device. |
"directory" | B_INT64_TYPE | ino_t (node number) of the directory. |
"node" | B_INT64_TYPE | ino_t of the new entry's node. |
"name" | B_STRING_TYPE | The name of the new entry. |
The "device", "node", and "name" fields are the same as for B_ENTRY_CREATED, plus...
Field | Type | Description |
---|---|---|
"from_directory" | B_INT64_TYPE | The ino_t number of the old directory. |
"to_directory" | B_INT64_TYPE | The ino_t number of the new directory. |
The B_ENTRY_REMOVED message takes the same form as B_ENTRY_CREATED, but without the "name" field. This, obviously, can be a problem—what good is it if you're told that a file has been removed, but you're not told the file's name? In some cases, simply being told that a file has been removed actually is good enough: You can simply re-read the contents of the directory.
|
Creates a new BDirectory object that represents the directory as given by the arguments. See the analogous SetTo() functions for descriptions of the flavorful constructors.
To check to see if an initialization was successful, call InitCheck().
|
Deletes the object.
|
Returns true if path or entry is contained within this directory, or in any of its subdirectories (no matter how deep). You can use the nodeFlags argument to limit the search to a particular flavor of node:
|
These functions create a new file, directory, or symbolic link. The new node is located at path. If path is relative, it's reckoned off of the directory represented by this BDirectory; if it's absolute, the path of this BDirectory is ignored.
The object argument (the BDirectory, BFile, or BSymLink) may be NULL. If the function fails, the object argument, if non-NULL, is Unset().
RETURN CODES
|
Finds the entry with the given name, and sets the second argument to refer to that entry.
If path isn't found, the second argument is automatically Unset(). To find out why the lookup failed, invoke InitCheck() on the entry argument:
BEntry entry; status_t err; if (dir.FindEntry("aFile", &entry) != B_OK) { err = entry.InitCheck(); }
The direct return value is also informative, but it may not be as precise as the InitCheck() value.
RETURN CODES
|
Initializes entry to represent this BDirectory. If the initialization fails, entry is Unset().
RETURN CODES
|
The three GetNext...() functions retrieve the "next" entry that lives in the BDirectory and returns it as a BEntry, entry_ref, or dirent structure.
|
GetNextEntry() and GetNextRef() are reasonably clear; the dirent version deserves more explanation. You'll find this explanation (and an example) in the BEntryList class. Also, keep in mind that the set of candidate entries is different for the dirent version: GetNextDirents() finds all entries, including the entries for "." and "..". The other two versions skip these entries.
When you're done reading the BDirectory's entries, you can rewind the object's entry iterator by calling Rewind().
CountEntries() returns the number of entries (not counting "." and "..") in the directory.
|
RETURN CODES
|
Gets the stat structure for the entry designated by path. path may be either absolute or relative; if it's relative, it's reckoned off of the BDirectory's directory. This is, primarily, a convenience function; but it's also provided for efficiency.
RETURN CODES
|
Returns true if this BDirectory represents a root directory. A root directory is the directory that's at the root of a volume's file hierarchy. Every volume has exactly one root directory; all other files in the volume's hierarchy descend from the root directory.
|
Closes the BDirectory's current directory (if any), and initializes the object to open the directory as given by the arguments.
If the specification results in a symbolic link that resolves to a directory, then the linked-to directory is opened. If the specification is (or resolves to) a regular file, the initialization fails.
RETURN CODES
|
In the expression
BDirectory a = b;
BDirectory a is initialized to refer to the same directory as b. To gauge the success of the assignment, you should call InitCheck() immediately afterwards. Assigning a BDirectory to itself is safe.
Assigning from an uninitialized BDirectory is "successful": The assigned-to BDirectory will also be uninitialized (B_NO_INIT).
|
Creates all missing directories along the path specified by path.
RETURN CODES
Declared in: be/storage/FindDirectory.h
|
|
Finds the path to the directory symbolized by which and copies it into path_string, or uses it to initialize path_obj.
The directory_which constants are described in the "Global Constants and Defined Types" section at the end of this chapter.
RETURN CODES
The Storage Kit Table of Contents | The Storage Kit Index |
Copyright © 2000 Be, Inc. All rights reserved..